//
// Created by linyongzhi on 2023/12/8.
//

#ifndef PC_SDK_DEMO_CPHH264DECODER_H
#define PC_SDK_DEMO_CPHH264DECODER_H

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}

#include <thread>
#include <mutex>
#include <queue>
#include "CPHBuffer.h"

class CPHBufferQueue;

class CPHH264Decoder {
public:
    int Init();
    int DecodeOneFrame();
    int DeInit();

    void SetYuvQueue(CPHBufferQueue *yuvQueue);
private:
    static void DecodeTaskEntry(CPHH264Decoder *decoder);

private:
    AVFormatContext *pFormatCtx = NULL;
    int videoStream = -1;
    AVCodecParameters *pCodecParam = NULL;
    AVCodecContext *pCodecCtx = NULL;
    const AVCodec *pCodec = NULL;
    AVFrame *pFrame = NULL;
    AVFrame *pFrameYUV = NULL;
    AVPacket packet {};
    struct SwsContext *sws_ctx = NULL;

    int numBytes = -1;
    uint8_t *buffer = NULL;
    bool taskRunning = false;
    std::thread *decodeThread {nullptr};

    CPHBufferQueue *m_yuvQueue;
};


#endif //PC_SDK_DEMO_CPHH264DECODER_H
